function checkAll(myform) {
	for (var i=0;i<myform.elements.length;i++) {
		var e = myform.elements[i];
		if ((e.name != 'checkall') && (e.type=='checkbox') && (!e.disabled) ) {
			e.checked = myform.checkall.checked;
		}
	}
}

function checkAllById(myform, checkallElement, elementId) {
	for (var i=0;i<myform.elements.length;i++) {
		var e = myform.elements[i];
		if ((e.name != 'checkall') && (e.type=='checkbox') && (!e.disabled) && e.id == elementId) {
			e.checked = checkallElement.checked;
		}
	}
}

function ComputeTotalAvgp(htmlForm) {
    var totalFleetAvgp = 0
    var totalArmyAvgp = 0

    for (var i=0; i < htmlForm.elements.length; ++i) {
        inputField = htmlForm.elements[i];
        inputId = inputField.id;
        var nbItems = parseInt(inputField.value, 10);

        if (!isNaN(nbItems)) {
	        if (inputId.substring(0,4) == "army") {
	        	startAvgp = inputId.lastIndexOf("_");
	        	if (startAvgp >= 0) {
		            armyAvgp = parseInt(inputId.substring(startAvgp + 1), 10);
		            totalArmyAvgp += nbItems * armyAvgp;
	        	}
	        	
	        } else if (inputId.substring(0,4) == "ship") {
	        	startAvgp = inputId.lastIndexOf("_");
	        	if (startAvgp >= 0) {
	        		fleetAvgp = parseInt(inputId.substring(startAvgp + 1), 10);
	                totalFleetAvgp += nbItems * fleetAvgp;
	        	}
	        }
        }
    }

    var divFleet = document.getElementById('fleet_total_avgp');
    var divFleetValue = document.getElementById('fleet_total_avgp_value');
    var divArmy = document.getElementById('army_total_avgp');
    var divArmyValue = document.getElementById('army_total_avgp_value');
    
    if (totalFleetAvgp > 0) {
    	divFleetValue.innerHTML = numberFormat(totalFleetAvgp);
    	divFleet.style.display='block';
    } else {
    	divFleet.style.display='none';
    }
    
    if (totalArmyAvgp > 0) {
    	divArmyValue.innerHTML = numberFormat(totalArmyAvgp);
    	divArmy.style.display='block';
    } else {
    	divArmy.style.display='none';
    }
}

function numberFormat(numberToFormat) {
     var formatedNumber = "";

     tmpStr = "" + numberToFormat;

     if(tmpStr.length > 3) {
    	 while(tmpStr.length > 3) {
    		 formatedNumber = "," + tmpStr.substr(tmpStr.length - 3) + formatedNumber;
             tmpStr = tmpStr.substr(0,tmpStr.length - 3);
    	 }
    	 formatedNumber = tmpStr + formatedNumber;
     
     } else {
    	 formatedNumber = tmpStr;
     }
     
     return formatedNumber;
} 

function ShowHideDiv(elementId) {
	var element = document.getElementById(elementId);
	
	if(element.style.display != 'block') {
	    element.style.display = 'block';
	} else {
	    element.style.display = 'none';
	}
	
	return true;
}

function SetCookie(cookieName, cookieValue) {
	document.cookie = cookieName + "=" + cookieValue + ";path=/";
}
